import FieldsUtils from '@/utils/fields';
import { Space } from 'antd';
import dayjs from 'dayjs';
import { ReactNode } from 'react';

// 处理表格字段
const getTableFieldsList = (fields: FieldProps[], methods?: any) => {
  const custom = {
    updatetime: {
      render: (text: ReactNode, record: any) => {
        // 检查是否有时间戳数据
        if (record.updatetime) {
          // 如果是时间戳（数字），转换为时间格式
          if (typeof record.updatetime === 'number') {
            // 秒级时间戳需要乘以1000转换成毫秒级
            return dayjs(record.updatetime * 1000).format('YYYY-MM-DD HH:mm:ss');
          }
          // 如果是字符串，尝试解析
          return dayjs(record.updatetime).format('YYYY-MM-DD HH:mm:ss');
        }
        return '-';
      },
    },
    // 操作栏
    operate: {
      width: 120,
      render: (text: ReactNode, record: any) => (
        <Space wrap>
          <a key="edit" className="editA" onClick={() => methods.onModal(record, 'update')}>
            编辑
          </a>
        </Space>
      ),
    },
  };

  let _fields = FieldsUtils.getColumns(fields, custom);
  _fields = FieldsUtils.filterByType(_fields, 'list');
  return _fields;
};

export { getTableFieldsList };
